/*
 * Copyright (c) 2017. danlu.com Co.Ltd. All rights reserved.
 */

package cn.com.dhc.danlu.dlware.net;

import android.app.ProgressDialog;
import android.content.Context;

import com.danlu.lib.net.exception.ObviousException;

/**
 * author: wuhaiyang(<a href="mailto:wuhaiyang@danlu.com">wuhaiyang@danlu.com</a>)<br/>
 * version: 1.0.0<br/>
 * since: 2017-02-07 上午10:57<br/>
 * <p>
 * 加载进度交互 订阅者 封装
 * </p>
 */
public abstract class ProgressObserver <T> extends BaseObserver<T> {
    private Context context;
    private boolean isShowLoaddingDialg;
    private int loaddingMsgId;
    private boolean cancelable;

    private ProgressDialog dialog;

    public ProgressObserver (Context context, boolean isShowLoaddingDialg, int loaddingMsgId, boolean cancelable) {
        super ();
        this.context = context;
        this.isShowLoaddingDialg = isShowLoaddingDialg;
        this.loaddingMsgId = loaddingMsgId;
        this.cancelable = cancelable;
    }

    public ProgressObserver () {

    }

    public ProgressObserver (Context context, int loaddingMsgId) {
        this (context, true, loaddingMsgId, false);
    }

    @Override
    public void onStart () {
        super.onStart ();
        if (isShowLoaddingDialg) {
            //            PromptManager.showDialog(context, loaddingMsgId, cancelable);
            showDialog ();
        }
    }

    protected void showDialog () {
        dialog = ProgressDialog.show (context, null, context.getString (loaddingMsgId), true, cancelable);
    }

    protected void dismissDialog () {
        if (null != dialog && dialog.isShowing ()) {
            dialog.dismiss ();
        }
    }

    @Override
    protected void onError (ObviousException ex) {
        dismissDialog ();
        onAlertTips (ex.getDisplayMessage ());
    }

    /**
     * use this method replace protected onError or public onError
     */
    protected void onAlertTips (String displayMessage) {
    }

    @Override
    public void onCompleted () {
        dismissDialog ();
    }
}
